package com.atguigu.controller;

import java.io.*;

import com.atguigu.pojo.SysUser;
import com.atguigu.service.DeliveryService;
import com.atguigu.service.impl.DeliveryServiceImpl;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.WebServlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Map;
import java.util.Set;

@WebServlet("/delivery/add")
public class DeliveryAddController extends HttpServlet {
    private DeliveryService deliveryService = new DeliveryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        /*
          2.将当前登录的用户id获取出来
            因为添加记录的时候我们要知道是谁添加的
         */
        SysUser sysUser = (SysUser) request.getSession().getAttribute("user");
        Integer uid = sysUser.getId();
        System.out.println(uid);
        /**
         * 获取请求参数
         */
        Map<String, String[]> parameterMap = request.getParameterMap();
        Set<Map.Entry<String, String[]>> set = parameterMap.entrySet();
        for (Map.Entry<String, String[]> entry : set) {
            System.out.println(entry.getKey() + ":" + Arrays.toString(entry.getValue()));
        }
        /**
         * 将参数提交到service层
         */
        try {
            deliveryService.addDelivery(uid, parameterMap);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        //5.重定向重新给查询快递信息功能发请求,重新查询
        response.sendRedirect(request.getContextPath() + "/delivery_system/list");


    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}